import command.CommandExec
import scopt.OParser

import java.io.File
import implicits.Implicits._
import tool.Tool

import java.nio.file.Files

/**
 * Created by yz on 10/4/2021
 */

case class Config(queryFile: File = new File("G:\\willInsertGbffs\\SAMN02469331.gbff"),
                  targetFile: File = new File("C:\\vg_p3_database\\test\\mummer_example\\target_SAMEA1705914.gbff"),
                  outDir: File = new File("C:\\mummer_database\\mummer_out")
                 )

object Main {

  def main(args: Array[String]): Unit = {
    val builder = OParser.builder[Config]
    val parser1 = {
      import builder._
      //q t o
      OParser.sequence(
        programName("scopt"),
        head("scopt", "4.x"),
        opt[File]('q', "query").valueName("<file>").required().action { (x, c) =>
          c.copy(queryFile = x)
        }.text("please input query gbff file!"),
        opt[File]('t', "target").valueName("<file>").required().action { (x, c) =>
          c.copy(targetFile = x)
        }.text("please input target gbff file!"),
        opt[File]('o', "out").valueName("<file>").required().action { (x, c) =>
          c.copy(outDir = x)
        }.text("please input out directory!"),
        help("help").text("prints this usage text")
      )
    }
    OParser.parse(parser1, args, Config()) match {
      case Some(config) => exec(config)
      case _ =>
    }

    def exec(config: Config) = {
      inputCheck(config)
      val querySampleFile = config.queryFile
      val refSampleFile = config.targetFile
      val querySampleName = querySampleFile.namePrefix
      val refSampleName = refSampleFile.namePrefix
      val workspaceDir = Tool.createTempDirectory("tmpDir")
      List(querySampleFile, refSampleFile).foreach { file =>
        val destFile = new File(workspaceDir, s"${file.getName.fileNamePrefix}.gbff")
        if (!destFile.exists()) file.copyTo(destFile)
      }
      val resultDir = config.outDir.createDirectoryWhenNoExist
      val commandExec = CommandExec().exec { b =>
        Tool.readGeneBanks(workspaceDir)
      }.exec { b =>
        Tool.genePredicts(workspaceDir)
      }.exec { b =>
        Tool.mummer(workspaceDir, resultDir, querySampleName, refSampleName)
      }.map { b =>
        Tool.mummerResultDeal(workspaceDir, resultDir, refSampleName)
      }
      workspaceDir.deleteQuietly
    }

    def inputCheck(config: Config) = {
      val querySampleFile = config.queryFile
      val refSampleFile = config.targetFile
      val queryFileSuffix = querySampleFile.fileNameSuffix
      val refFileSuffix = refSampleFile.fileNameSuffix
      val canAcceptedSuffixs = List("gbff", "gbk", "gb")
      if (!canAcceptedSuffixs.contains(queryFileSuffix)) {
        System.err.println("Error:Query(查询样本)文件应为GeneBank格式（*.gbk,*.gb,*.gbff）！")
        System.exit(-1)
      }
      if (!canAcceptedSuffixs.contains(refFileSuffix)) {
        System.err.println("Error:Target(目标样本)文件应为GeneBank格式（*.gbk,*.gb,*.gbff）！")
        System.exit(-1)
      }
      val querySampleName = querySampleFile.namePrefix
      val refSampleName = refSampleFile.namePrefix
      if (querySampleName == refSampleName) {
        System.err.println("Error:查询样本文件名和目标样本文件文件名不能相同！")
        System.exit(-1)
      }
    }

  }


}
